package service.article;

import dao.article.ArticleDao;
import dao.article.ArticleDaoImpl;
import domain.Article;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ArticleServiceImpl implements ArticleService {
    ArticleDao articleDao = new ArticleDaoImpl();
    /**
     * 新增文章
     * @param type
     * @param title
     * @param info
     * @param abs
     * @param author
     * @param state
     * @return int
     * @author 夏添
     * @date 2020/9/17 17:28
     */
    public int creatArticle(int type,String title,String info,String abs,String author,int state){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String change_time = dateFormat.format(date);
        int i = 0;
        try {
            i = articleDao.creatArticle(type,title,info,abs,change_time,author,state);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 后台获取文章列表
     * @return List<Article>
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public List<Article> getArticleList(){
        List<Article> list = new ArrayList<>();
        try {
            list = articleDao.getArticleList();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取文章详情
     * @param id
     * @return Artlcle
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public Article getArticleInfo(int id){
        Article article =new Article();
        try {
            article = articleDao.getArticleInfo(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return article;
    }

    /**
     * 前台获取文章列表
     * @param type
     * @return List<Article>
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public List<Article> getNewsList(int type){
        List<Article> list = new ArrayList<>();
        try {
            list = articleDao.getNewsList(type);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 编辑文章
     * @param id
     * @param type
     * @param title
     * @param info
     * @param abs
     * @param author
     * @param state
     * @return int
     * @author 夏添
     * @date 2020/9/17 17:28
     */
    public int changeArticle(int id,int type,String title,String info,String abs,String author,int state){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String change_time = dateFormat.format(date);
        int i = 0;
        try {
            i = articleDao.changeArticle(id,type,title,info,abs,change_time,author,state);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 删除文章
     * @param id
     * @return int
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public int DeleteArticle(int id){
        int i =0;
        try {
            i = articleDao.DeleteArticle(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
}
